package com.xiaouyudeguang.common.service;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xiaouyudeguang.common.collect.CollectContext;
import com.xiaouyudeguang.common.dao.AbstractDao;
import com.xiaouyudeguang.common.request.RequestDto;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

public interface AbstractService<Dao extends AbstractDao<T>, T> extends CollectContext, IService<T> {

    /**
     * 根据id查询
     *
     * @return
     */
    T getById(String tenantId, String appId, String id) throws Exception;

    /**
     * 批量查询
     *
     * @param tenantId
     * @param appId
     * @param ids
     * @return
     * @throws Exception
     */
    List<T> getByIds(String tenantId, String appId, Set<String> ids) throws Exception;

    /**
     * 根据id删除
     */
    Boolean deleteById(String tenantId, String appId, String id) throws Exception;

    /**
     * 根据id删除
     */
    Boolean deleteByIds(String tenantId, String appId, Set<String> ids) throws Exception;

    Boolean delete(String tenantId, String appId, SFunction<T, ?> function, Object value) throws Exception;

    Boolean delete(String tenantId, String appId, SFunction<T, ?> function, Set<?> values) throws Exception;

    /**
     * 根据id更新
     *
     * @return
     */
    T updateById(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    List<T> updateByIds(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    List<T> updateBatch(String tenantId, String appId, List<T> dataList) throws Exception;

    T modify(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    /**
     * 添加
     *
     * @return
     */
    T save(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    /**
     * 批量添加
     *
     * @return
     */
    List<T> saveBatch(String tenantId, String appId, List<T> dataList) throws Exception;

    /**
     * 单条查询
     *
     * @return
     */
    T getOne(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    /**
     * 单条查询
     *
     * @return
     */
    Boolean exist(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    /**
     * 单条查询
     *
     * @return
     */
    T getOne(String tenantId, String appId, SFunction<T, ?> function, Object value) throws Exception;

    /**
     * 列表查询
     *
     * @return
     */
    List<T> list(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    List<T> tree(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    List<T> list(String tenantId, String appId, SFunction<T, ?> function, Object value) throws Exception;

    List<T> like(String tenantId, String appId, SFunction<T, ?> function, Object value) throws Exception;

    List<T> list(String tenantId, String appId, SFunction<T, ?> function, Set<?> values) throws Exception;

    List<T> listAll(String tenantId, String appId) throws Exception;

    List<T> listByMap(String tenantId, String appId, Map<String, Object> params) throws Exception;

    /**
     * 分页查询
     *
     * @return
     */
    PageImpl<T> page(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;


    Page<T> pageAll(String tenantId, String appId, RequestDto<T> requestDto) throws Exception;

    default PageImpl<T> emptyPage() {
        return page(Collections.emptyList(), 0);
    }

    default PageImpl<T> page(List<T> records, long total) {
        return new PageImpl<>(records, Pageable.unpaged(), total);
    }

    /**
     * 批量添加
     *
     * @return
     */
    List<T> upload(String tenantId, String appId, List<T> dataList) throws Exception;

    List<T> export(String tenantId, String appId, RequestDto<T> doFill) throws Exception;

    void encrypt(String tenantId, String appId) throws Exception;
}
